<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>面经</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <template #header>
        <el-row type="flex" justify="space-between">
          <el-col :span="3">
            <span>共{{ total }}条记录</span>
          </el-col>
          <el-col :span="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-plus"
              round
              @click="(article = {}), (dialogVisible = true)"
              >添加面经</el-button
            >
          </el-col>
        </el-row>
      </template>
      <el-table :data="rows">
        <!-- el-table-column 有一列就要一个 -->
        <!-- label 表格列的标题
        prop 表格列要用动态数据中哪个属性 -->
        <!-- width 不加的默认是由内容撑开的宽度 -->
        <el-table-column label="编号">
          <template #default="scope">
            <!-- {{(当前页-1)*每页条数+索引+1}} -->
            {{ (params.current - 1) * params.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="标题"
          prop="stem"
          width="300px"
        ></el-table-column>
        <el-table-column label="作者" prop="creator"></el-table-column>
        <el-table-column label="点赞" prop="likeCount"></el-table-column>
        <el-table-column label="浏览数" prop="views"></el-table-column>
        <el-table-column label="更新时间" prop="createdAt"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" icon="el-icon-view" @click="showDrawer(scope.row.id)"></el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="edit(scope.row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center">
        <el-pagination
          background
          layout="total,prev, pager, next,sizes,jumper"
          :total="total"
          :page-size="params.pageSize"
          :page-sizes="[5, 10, 20, 30]"
          @current-change="handleChangeCurrent"
          @size-change="handleChangeSize"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 组件包起来的是插槽的东西 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="dialogVisible"
      width="70%"
      @close="cancel"
    >
      <el-form
        label-width="80px"
        :model="article"
        :rules="articleRules"
        ref="articleForm"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="article.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="article.content"
            @blur="$refs.articleForm.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleConfirm">确认</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-drawer title="查看" :visible.sync="drawer" direction="rtl" size="60%">
       <h3>{{article.stem}}</h3>
       <div v-html="article.content"></div>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  getArticles,
  delArticle,
  addArticle,
  getArticle,
  updateArticle
} from '@/api/article'
export default {
  components: {
    quillEditor
  },
  name: 'ArticlePage',
  async created () {
    await this.getArticles()
  },
  data () {
    return {
      article: {
        stem: '',
        content: ''
      },
      articleRules: {
        stem: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      dialogVisible: false, // 对话框刚开始是隐藏的
      total: null,
      rows: [],
      params: {
        current: 1, // 当前页
        pageSize: 5 // 每页显示的条数
      },
      isEdit: false, // 默认情况下 不是编辑中
      drawer: false
    }
  },
  methods: {
    async getArticles () {
      const res = await getArticles(this.params)
      console.log(res)
      this.total = res.data.total
      this.rows = res.data.rows
    },
    async handleChangeCurrent (current) {
      console.log(current)
      this.params.current = current
      await this.getArticles()
    },
    async handleChangeSize (pageSize) {
      this.params.pageSize = pageSize
      await this.getArticles()
    },
    async del (id) {
      try {
        await this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 这一行代码只有在用户点击了确认之后才有机会执行
        console.log(id)
        await delArticle(id)
        this.$message.success('删除成功')
        await this.getArticles()
      } catch (e) {
        console.log(e)
      }
    },
    cancel () {
      this.dialogVisible = false
      // 重置表单
      this.$refs.articleForm.resetFields()
    },
    async handleConfirm () {
      // 二次校验
      try {
        await this.$refs.articleForm.validate()
        if (this.isEdit) {
          // 编辑
          const obj = {
            id: this.article.id,
            stem: this.article.stem,
            content: this.article.content
          }
          await updateArticle(obj)
          this.$message.success('编辑成功')
        } else {
          // 添加
          await addArticle(this.article)
          this.$message.success('添加成功')
        }
        this.dialogVisible = false
        await this.getArticles()
      } catch (e) {
        console.log(e)
      }
    },
    async edit (id) {
      // 数据回写
      const res = await getArticle(id)
      console.log(res)
      this.article = res.data
      this.isEdit = true
      this.dialogVisible = true
    },
    async showDrawer (id) {
      const res = await getArticle(id)
      this.article = res.data
      console.log(this.article)
      this.drawer = true
    }
  }
}

// function fn (obj) {
//   console.log(obj.a)
// }

// fn({ a: 2, b: 3 })
</script>

<style scoped lang='scss'>
.el-card {
  box-shadow: none;
  margin-top: 10px;
}

::v-deep .ql-editor {
  min-height: 200px;
}
</style>
